﻿using System.Data.SqlClient;
using KMF.Common;
using System.Data;
using System.Collections.Generic;

namespace KMF.DAL
{
    public partial class UserDb
    {
        public static List<UserPagingDto> GetAllUserPaging(int page, int itemsPerPage, ref int totalPage)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_User_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<UserPagingDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<UserPagingDto> GetAllUserPagingByKeyword(int page, int itemsPerPage, ref int totalPage, string keyword)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_User_GetSearchPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                command.Parameters.Add(dao.CreateParameter("@keyword", keyword));
                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<UserPagingDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }
        public static UserDto GetAdminByUserName(string username)
        {
            var dao = SharedSqlDao;
            var commad = dao.GetDbSprocCommand("WSPX_User_SelectByUserName");
            try
            {
                commad.Parameters.Add(dao.CreateParameter("@UserName", username));
                var result = dao.GetSingleDTO<UserDto>(ref commad);
                return result;
            }
            finally
            {
                commad.Connection.Close();
            }
        }

        public static UserDto GetByUserName(string userName)
        {
            var dao = SharedSqlDao;
            SqlCommand command = dao.GetDbSprocCommand("WSPX_Users_GetByUserName");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@UserName", userName));
                return dao.GetSingleDTO<UserDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }//End method

    }
}
